import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Delete from '#cpt/temp/Delete'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
const freezeData = [{name:'启用',value:'1'},{name:'禁用',value:'0'}]
const statusData = [{name:'启用',value:'1'},{name:'禁用',value:'2'}]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '模板名称',		name: 'model_name' },
			{ label: '模板编码',		name: 'model_code' },
			{ label: '状态',		name: 'status',	type: 'select', data:statusData },
		],
		// 提交表单列表
		formData : [
			{ label: '模板名称',		name: 'model_name', 			half:true, required:true },
			{ label: '状态',		name: 'status', 					half:true, required:true, type: 'select', data:statusData,	value:'1' },
			{ label: '冻结首行',		name: 'freeze_rows', 			half:true, type: 'select', data:freezeData,	value:'1' },
			{ label: '冻结首列',		name: 'freeze_columns', 		half:true, type: 'select', data:freezeData,	value:'0' },
			{ label: '合并自然项目',	name: 'merge_parent_kind_name', half:true, type: 'checkbox' },
			{ label: '复制模板',		name: 'copy_model_code', 		half:true, type: 'select', loading:true, idStr:'model_code', nameStr:'model_name' },
			{ label: '描述',		name: 'model_description', 			full:true, type:'textarea' },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.getSelect()
		this.fetch()
	}
	//获取下拉数据
	getSelect = () => {
		const { formData } = this.state
		
		$cache.then( async f => {
			// 复制模板
			$fn.setRow( formData, 'copy_model_code', await f.getCache( { api:'export_template', way:'get',refresh:true } ))
			
			this.setState({ formData })
		})
	}
	// paging
	fetch = () => $fn.fetch.call(this,'settings/getExportModel', {isIndex:true})
	
	// 表单
	cols = [
		{ title: '模板名称',		dataIndex: 'model_name', 			width:150 },
		{ title: '模板编码', 	dataIndex: 'model_code', 			width:150 },
		{ title: '模板描述', 	dataIndex: 'model_description', 	width:150 },
		{ title: '状态', 		dataIndex: 'status', 				...$fn.table.status,  render: text => +text === 1 ? '启用' : '禁用' },
		{ title: '冻结首行', 	dataIndex: 'freeze_rows', 			...$fn.table.status,  render: text => +text === 1 ? '启用' : '禁用' },
		{ title: '冻结首列', 	dataIndex: 'freeze_columns', 		...$fn.table.status,  render: text => +text === 1 ? '启用' : '禁用' },
		{ title: '创建时间', 	dataIndex: 'created_at', 			...$fn.table.time },
		TableControls.controls.call(this, { width:200})
	]
	// 表格操作按钮
	table_controls = (text,rows) => {
		const { uuid } = rows
		const { formData } = this.state
		return [
			TableControls.edit.call(this, null, { uuid }, { rows, select:this.getModalSelect, click:()=>{
				$fn.hideRows(formData, 'copy_model_code', true)
				this.setState({formData})
			} }),							// 编辑
			this.onDel([ uuid ]),			// 删除
			TableControls.pushTo.call(this, '配置模板', `template-configuration?model_code=${rows.model_code}` )														// 配置模板
		]
	}
	
	// 操作按钮
	controls = () => {
		const { formData } = this.state
		return [
			Controls.add.call(this,{click:()=>{
				this.getSelect()
				$fn.hideRows(formData, 'copy_model_code', false)
				this.formRef.setValue({status:'1', freeze_rows:'1', freeze_columns:'0'})
				this.setState({formData})
			}}),								// 添加
			this.onDel(),						// 删除
		]
	}
	
	// 删除
	onDel = uuid => Delete.call(this, 'settings/delExportModel', { uuid, isKeys: !uuid, idStr:'uuids' })

	render(){
		return (
			<>
				<Page title='导出规则设置' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ api: 'settings/addExportModel', editApi:'settings/editExportModel' }) }
			</>
		)
	}
}

export default Index